﻿using System;
using System.Collections.Generic;

namespace Safebrowsing
{
    public class SafebrowsingList
    {
        /// <summary>
        /// Internal list identifier. MUST be unique.
        /// Do not use identifiers lesser than 100 for custom lists.
        /// </summary>
        public int ListId { get; private set; }

        /// <summary>
        /// List name. MUST be unique.
        /// </summary>
        public string Name { get; private set; }

        /// <summary>
        /// Safe browsing API service point. If null -- client uses default Google Safe Browsing API service point.
        /// </summary>
        internal SafebrowsingApiServicePoint ApiServicePoint { get; private set; }

        /// <summary>
        /// Creates an instance of the safe browsing list using default service point
        /// </summary>
        /// <param name="listId"></param>
        /// <param name="listName"></param>
        private SafebrowsingList(int listId, string listName) : this(listId, listName, null)
        {
        }

        /// <summary>
        /// Creates custom safe browsing list. Use this method if you're planning to use
        /// another API (not google) using the same protocol.
        /// </summary>
        /// <param name="listId"></param>
        /// <param name="listName"></param>
        /// <param name="apiServicePoint"></param>
        /// <exception cref="ApplicationException">Thrown when list with the specified listId is already created.</exception>
        public SafebrowsingList(int listId, string listName, SafebrowsingApiServicePoint apiServicePoint)
        {
            ListId = listId;
            Name = listName;
            ApiServicePoint = apiServicePoint;

            foreach (SafebrowsingList list in REGISTERED_LISTS)
                if (list.ListId == listId) throw new ApplicationException("List with this listId is already registered");

            REGISTERED_LISTS.Add(this);
        }

        #region Static fields and methods

        static SafebrowsingList()
        {
            PhishingList = new SafebrowsingList(1, "googpub-phish-shavar");
            MalwareList = new SafebrowsingList(2, "goog-malware-shavar");
        }

        private readonly static List<SafebrowsingList> REGISTERED_LISTS = new List<SafebrowsingList>();

        /// <summary>
        /// Default google phishing list (googpub-phish-shavar)
        /// </summary>
        public static SafebrowsingList PhishingList { get; private set; }

        /// <summary>
        /// Default google malware list (goog-malware-shavar)
        /// </summary>
        public static SafebrowsingList MalwareList { get; private set; }

        /// <summary>
        /// Searches registered lists by the list name.
        /// Returns null if list with such name is not found.
        /// </summary>
        /// <param name="listName"></param>
        /// <returns></returns>
        public static SafebrowsingList GetListByName(string listName)
        {
            foreach (SafebrowsingList list in REGISTERED_LISTS)
            {
                if (list.Name == listName) return list;
            }

            return null;
        }

        /// <summary>
        /// Searches registered lists by the list id.
        /// Returns null if list with such id is not found.
        /// </summary>
        /// <param name="listId"></param>
        /// <returns></returns>
        public static SafebrowsingList GetListById(int listId)
        {
            foreach (SafebrowsingList list in REGISTERED_LISTS)
            {
                if (list.ListId == listId) return list;
            }

            return null;
        }

        #endregion

        public override string ToString()
        {
            return Name;
        }
    }
}
